package com.gykfbxxh.imovie.greendao;

import com.gykfbxxh.imovie.AppApplication;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.bean.info.UserInfor;
import com.gykfbxxh.imovie.greendao.bean.LikeLookGD;
import com.gykfbxxh.imovie.greendao.bean.UserInforGD;
import com.gykfbxxh.imovie.greendao.gen.DaoMaster;
import com.gykfbxxh.imovie.greendao.gen.DaoSession;
import com.gykfbxxh.imovie.greendao.gen.LikeLookGDDao;
import com.gykfbxxh.imovie.greendao.gen.UserInforGDDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuxinghai
 * On 2017/8/21
 * GreenDao数据库管理类
 * 查询相关: eq相等   notEq 不相等，   gt大于   lt小于   ge大于等于   le 小于等于
 */

public class GDManager {
    private static final String DB_NAME = "gykfbxxh_db";
    private static GDManager mGDManager;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

    public GDManager() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(AppApplication.getInstance(),DB_NAME,null);
        mDaoMaster = new DaoMaster(openHelper.getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
    }

    /**
     * 获取单例
     */
    public static GDManager getInstance(){
        if(mGDManager==null) {
            mGDManager = new GDManager();
        }
        return mGDManager;
    }

    public DaoMaster getMaster() {
        return mDaoMaster;
    }

    public DaoSession getSession() {
        return mDaoSession;
    }

    /**
     * 用户数据插入或更新到数据库
     * @param userInfor
     */
    public void insertOrReplaceUserInforData(UserInfor userInfor){
        UserInforGD unique = getSession().getUserInforGDDao().queryBuilder().where(UserInforGDDao.Properties.FId.eq(userInfor.getfId())).unique();
        if(unique!=null) {
            unique.setFQqopenid(userInfor.getfQqopenid());
            unique.setFUsername(userInfor.getfQqopenid());
            unique.setFPhone(userInfor.getfQqopenid());
            unique.setFIntime(userInfor.getfQqopenid());
            unique.setFPassword(userInfor.getfQqopenid());
            unique.setFPhoto(userInfor.getfQqopenid());
            getSession().getUserInforGDDao().insertOrReplace(unique);
        }else {
            if(userInfor!=null) {
                long long_id = System.currentTimeMillis();
                UserInforGD userInforGD = new UserInforGD(long_id,
                        userInfor.getfId(),
                        userInfor.getfQqopenid(),
                        userInfor.getfUsername(),
                        userInfor.getfPhone(),
                        userInfor.getfIntime(),
                        userInfor.getfPassword(),
                        userInfor.getfPhoto()
                        );
                getSession().getUserInforGDDao().insertOrReplace(userInforGD);
            }
        }

        getUserInfoFromGD();
    }

    /**
     * 得到用户数据
     */
    public UserInfor getUserInfoFromGD(){
        List<UserInforGD> list = getSession().getUserInforGDDao().queryBuilder().build().list();
        if(list!=null&&list.size()>0) {
            UserInforGD userInforGD = list.get(list.size() - 1);
            if(userInforGD!=null) {
                UserInfor userInfor = new UserInfor();
                userInfor.setfId(userInforGD.getFId());
                userInfor.setfQqopenid(userInforGD.getFQqopenid());
                userInfor.setfUsername(userInforGD.getFUsername());
                userInfor.setfPhone(userInforGD.getFPhone());
                userInfor.setfIntime(userInforGD.getFIntime());
                userInfor.setfPassword(userInforGD.getFPassword());
                userInfor.setfPhoto(userInforGD.getFPhoto());
                return userInfor;
            }
        }
        return null;
    }

    /**
     * 清空用户信息
     */
    public void delUserInfoGD(){
        getSession().getUserInforGDDao().deleteAll();
    }

    private List<String> likeList = null ;

    /**
     * 获取喜欢收藏列表id
     * @return
     */
    public List<String> getLikeLookListData(){
        if(likeList==null) {
            likeList = new ArrayList<>();
        }
        likeList.clear();
        List<LikeLookGD> list = getSession().getLikeLookGDDao().queryBuilder().build().list();
        if(list!=null&&list.size()>0) {
            for (LikeLookGD l : list){
                likeList.add(l.getFObjectid());
            }
        }
        return likeList ;
    }
    /**
     * 获取喜欢收藏列表
     * @return
     */
    List<FilmEntity> filmEntityList = null ;
    public List<FilmEntity> getLikeDataForMine(){
        if(filmEntityList==null) {
            filmEntityList = new ArrayList<>();
        }
        filmEntityList.clear();
        List<LikeLookGD> list = getSession().getLikeLookGDDao().queryBuilder().build().list();
        if(list!=null&&list.size()>0) {
            for (LikeLookGD l : list){
                FilmEntity filmEntity = new FilmEntity();
                filmEntity.setFId(l.getFObjectid());
                filmEntity.setFPhoto(l.getImgUrl());
                filmEntity.setFName(l.getFname());
                filmEntityList.add(filmEntity);
            }
        }
        return filmEntityList ;
    }

    /**
     * 插入一条喜欢到数据库
     * @param filmEntity
     */
    public void insertLikeLookData(FilmEntity filmEntity){
        if(filmEntity==null) {
            return;
        }
        LikeLookGD likeData = getSession().getLikeLookGDDao().queryBuilder().where(LikeLookGDDao.Properties.FObjectid.eq(filmEntity.getFId())).build().unique();
        if(likeData!=null) {//为空代表用户之前收藏过
            return;
        }
        long l = System.currentTimeMillis();
        LikeLookGD likeLookGD = new LikeLookGD(l,filmEntity.getFId(),filmEntity.getFName(),filmEntity.getFPhoto());
        getSession().getLikeLookGDDao().insertOrReplace(likeLookGD);
    }

    /**
     * 删除一条喜欢
     */
    public void delLikeLookData(String objectId){
        LikeLookGD likeData = getSession().getLikeLookGDDao().queryBuilder().where(LikeLookGDDao.Properties.FObjectid.eq(objectId)).unique();
        if(likeData!=null) {
            getSession().getLikeLookGDDao().delete(likeData);
        }
    }

    /**
     * 获取指定id喜欢是否存在
     */
    public boolean getLikeById(String objectId){
        LikeLookGD unique = getSession().getLikeLookGDDao().queryBuilder().where(LikeLookGDDao.Properties.FObjectid.eq(objectId)).unique();
        if(unique!=null) {
            return true;
        }else {
            return false;
        }
    }

}
